;(function($) {
	
	function EzDialog(options) {
		this.instance = null;
		// 窗口
		this.dialog = null;
		// 内容
		this.content = null;
		// 标题
		this.title = null;
		// 关闭按钮
		this.closeBtn = null;
		// 高度
		this.height = null;
		// 宽度
		this.width = null;
		// 配置
		this.options = null;
		// 拖动中?
		this.dragging = false;
		this.offsetX = 0;
		this.offsetY = 0;
		
		this.moveables = null;
		
		this.index;
		
		this.initControl = function($self) {
			$.get($self.options.themeUrl, {}, function(result) {
				$self.dialog = $(result);
				$self.dialog.css('z-index', 8888+$self.index);
				$self.dialog.css('display', 'none');
				$self.title = $self.dialog.find('.kp-dlg-title');
				$self.title.width('auto');
				$self.title.append($self.options.title);
				$self.content = $self.dialog.find('.kp-dlg-content');
				$self.closeBtn = $self.dialog.find('.kp-dlg-btn-close');
				$self.moveables = $self.dialog.find('.kp-dlg-moveable');
				$self.initSize();
				$self.initPosition();
				$self.regDragEvent($self);
				$self.regCloseEvent($self);
				$self.regClickEvent();
				$self.fillContent($self);
				if(!EzDialog.shadow) {
					EzDialog.shadow = $('<div class="kp-dlg-shadow"></div>');
					EzDialog.shadow.css({
						'position': 'fixed',
						'height': '100%',
						'width': '100%',
						'left': '0',
						'top': '0',
						'background-color': '#000',
						'opacity': '0.3',
						'display': 'none'
					});
					if($('.kp-dlg-shadow').length == 0) {
						$('body').append(EzDialog.shadow);
					}
				}
				$('body').append($self.dialog);
				if($self.options.autoOpen) {
					$(document).ready(function() {
						$self.open();
					})
				}
			})
			
		}
		
		this.options = options;
		
		if(EzDialog.count != undefined) {
			++ EzDialog.count;
		} else {
			EzDialog.count = 0;
			$('.kp-dlg-btn-close').live('click', function() {
				$(this).closest('.kp-dlg').hide();
				EzDialog.shadow.hide();
			})
		}
		this.index = EzDialog.count;
		this.initControl(this);
	}
	
	EzDialog.prototype = {
		
		initSize: function() {
			this.height = this.options.minHeight ? this.options.minHeight : this.options.height;
			this.width = this.options.minWidth ? this.options.minWidth : this.options.width;
			if(this.options.minHeight) {
				this.dialog.css('minHeight', this.options.minHeight);
				this.dialog.height('auto');
			} else if(this.options.height) {
				this.dialog.height(this.options.height);
			}
			
			if(this.options.minWidth) {
				this.dialog.css('minWidth', this.options.minWidth);
				this.dialog.width('auto');
			} else if(this.options.width) {
				this.dialog.width(this.options.width);
			}
		},
		
		initPosition: function() {
			if(this.options.left) {
				dLeft = this.options.left;
			} else {
				dLeft = $(window).scrollLeft()+$(window).width()/2-parseInt(this.width)/2;
				if(dLeft < 0) {
					dLeft = 0;
				}
				dLeft += this.index * 20;
				dLeft += 'px';
			}
			if(this.options.top) {
				dTop = this.options.top;
			} else {
				dTop = $(window).scrollTop()+$(window).height()/2-parseInt(this.height)/2-100;
				if(dTop < 0) {
					dTop = 0;
				}
				dTop += this.index * 20;
				dTop += 'px';
			}
			
			this.dialog.css('left', dLeft);
			this.dialog.css('top', dTop);
		},
		
		fillContent: function($self) {
			// 正文内容填充
			$target = null;
			if(this.options.target) {
				$target = $(this.options.target);
				this.content.html($target.html())
			} else if(this.options.url) {
				$.get(this.options.url,{}, function(result) {
					$self.content.html(result);
				});
			} else if(this.options.content) {
				this.content.html(this.options.content);
			}
		},
		
		
		open: function() {
			if(!EzDialog.shadow.is(':visible')) {
				EzDialog.shadow.show();
			}
			this.dialog.show();
		},
		
		// 拖动事件
		regDragEvent: function($self) {
			$(this.moveables).mousedown(function(event){
				$self.dragging = true;
				$self.offsetX = event.pageX - $self.dialog.offset().left;
				$self.offsetY = event.pageY - $self.dialog.offset().top;       
				$(this).css('cursor','move');
				
			})
			.mouseup(function(event){
				$self.dragging = false;
				$(this).css('cursor','pointer');
			});
			
			$(document).mousemove(function(event){            
				if(!$self.dragging)
					return;
				x = event.clientX-$self.offsetX/*+$(window).scrollLeft()*/;            
				y = event.clientY-$self.offsetY/*+$(window).scrollTop()*/;           
				$self.dialog.css({"left": x, "top": y});      
			});
		},
		
		regClickEvent: function() {
			this.dialog.mousedown(function() {
				$('.kp-dlg').css('z-index', '10000');
				$(this).css('z-index', '10001');
			})
		},
		
		// 关闭事件
		regCloseEvent: function($self) {
			this.dialog.find('.kp-dlg-btn-close').click(function(e) {
				if($self.options.askOnClose) {
					if(!confirm($self.options.askOnClose)) {
						return;
					}
				}
				$self.close();
			});
		},
		
		close: function() {
			this.dialog.hide();
			if($('.kp-dlg:visible').length == 0) {
				EzDialog.shadow.hide();
			}
		}
	}
		
	$.extend({
		'EzDialog': function(options) {
			return new EzDialog(options);
		}
	})
})(jQuery);
